<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>036-设计模式-发布订阅者模式.html</title>
	<script>
		(function(w){
			function Teacher(name){
				this.name = name;
				this.students = [];
			}
			Teacher.prototype = {
				constructor:Teacher,
				register:function(student){
					this.students.push(student);
				},
				publish:function(task){
					for(var i = 0;i<this.students.length;i++){
						this.students[i].listen(task);
					}
				}
			}
			w.Teacher = Teacher;
		})(window);

		(function(w){
			function Student(name,teacher){
				this.name = name;
				// teacher.Students.push(this)
				teacher.register(this)
			}
			Student.prototype = {
				constructor:Student,
				listen:function(task){
					console.log(this.name + 'got task:' + task);
				}
			}
			w.Student = Student;
		})(window);
		var t1 = new Teacher('Chris');

		var s1 = new Student('cdc',t1);
		var s2 = new Student('qwe',t1);
		t1.publish('coding...');
		// console.log(t1);
		// console.log(s1);
	</script>
</head>
<body>
	
</body>
</html>